package org.flexlancer.CommentsLibrary
{
	import flash.events.MouseEvent;
	
	import mx.containers.Panel;
	import mx.containers.VBox;
	import mx.controls.Label;
	import mx.controls.LinkButton;
	import mx.controls.TextArea;
	import mx.core.ScrollPolicy;
	
	import org.flexlancer.Logger.Logger;
	import org.flexlancer.events.CommentItemEvent;

	public class CommentItem extends VBox
	{
		/*
		Variables
		**/
		private var _comment_panel:Panel;
		private var _messege:String;
		private var _user_name:String;
		private var _date:String;
		
		private var _taMessage:TextArea;
		private var _lbDate:Label;
		private var _lbReply:LinkButton; 
		/*
		Constructors
		**/
		public function CommentItem(parmUserName:String,parmMessege:String,parmDate:Date):void{
			try{
				super();
				this.setStyle("left",0);
				this.setStyle("top",0);
				this.setStyle("right",0);
				this.setStyle("bottom",0);
				this.AddComment(parmUserName,parmMessege,parmDate);
				
			}catch(err:Error){
				Logger.LogE(err);
			}
		}
		/*
		Properties
		**/
		/*
		Implementation
		**/
		private function OnReplyClick(event:MouseEvent):void{
			try{
				//this.AddComment();
				var send_event:CommentItemEvent = new CommentItemEvent(CommentItemEvent.REPLY_CLICK);
				send_event.CommentUser = this._user_name;
				send_event.CommentText = this._messege;
				this.dispatchEvent(send_event);
			}catch(err:Error){
				Logger.LogE(err);
			}
		}
		
		public function getXMLData():XML {
			try{
				var retXML:XML = new XML(<commentitem/>);
				
				retXML.messege = this._taMessage.text;
				retXML.username = this._user_name;
				retXML.date = this._date;
				
				return retXML;
			}catch(err:Error){
				throw err;
			}
			return null;
		}
		private function AddComment(parmUserName:String,parmMessege:String,parmDate:Date):void{
			try{
					
				
					this._user_name = parmUserName;
					//
					
					//
					if(parmDate == null) {
					var _now:Date = new Date();
					this._date = _now.toLocaleDateString() + " "+_now.toLocaleTimeString();
					
					} else {
						this._date = parmDate.toLocaleDateString() + " "+ parmDate.toLocaleTimeString();
					}
					this._comment_panel = new Panel();
					this._comment_panel.verticalScrollPolicy = ScrollPolicy.OFF;
					
					//this._comment_panel.percentWidth = 100;
					//this._comment_panel.percentHeight = 100;
					
					this._comment_panel.setStyle("left",0);
					this._comment_panel.setStyle("top",0);
					this._comment_panel.setStyle("right",0);
					this._comment_panel.setStyle("bottom",0);
					//this._comment_panel.setStyle("headerHeight",40);
					this._comment_panel.title = _user_name;// + "\n" + _date;
					 
					this._taMessage = new TextArea();
					
					//////// shahid
					//this._taMessage.editable = true;
					////////
					this._lbDate = new Label();
					this._lbDate.text = _date;
					this._lbDate.setStyle("left",0);
					this._lbDate.setStyle("top",0);
					this._comment_panel.addChild(this._lbDate);
					
					this._taMessage.setStyle("left",0);
					this._taMessage.setStyle("top",0);
					//this._taMessage.setStyle("right",0);
					this._taMessage.percentWidth = 100;
					this._taMessage.setStyle("bottom",20);
					this._taMessage.text = parmMessege;
					this._taMessage.addEventListener(MouseEvent.CLICK,OnTextClick);
					this._comment_panel.addChild(this._taMessage);
					
					/// shahid code
					
					//this._taMessage.getFocus();
					///
					this._lbReply = new LinkButton();
					this._lbReply.setStyle("right",0);
					this._lbReply.setStyle("bottom",0);
					this._lbReply.label = "Reply";
					this._lbReply.addEventListener(MouseEvent.MOUSE_DOWN,OnReplyClick);
					this._comment_panel.addChild(this._lbReply);
					
					this.addChild(_comment_panel);
				}catch(err:Error){
					Logger.LogE(err);
				}
		}
		private function OnTextClick(evt:MouseEvent):void{
			try{
				evt.stopPropagation();				
			}catch(err:Error){
					Logger.LogE(err);
			}
		}
	}
}